package org.needcoke.hs.httpclient;

import org.apache.http.*;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.utils.URIBuilder;
import org.needcoke.hs.http.ContentType;
import org.needcoke.hs.http.HttpType;
import org.needcoke.hs.http.Request;
import org.apache.http.protocol.HttpContext;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class HttpClientRequest implements Request {

    private HttpRequest httpRequest;

    private HttpContext httpContext;

    @Override
    public String getHost() {
        return null;
    }

    @Override
    public String getUrl() {
        RequestLine requestLine = httpRequest.getRequestLine();
        return requestLine.getUri();
    }

    @Override
    public void setUrlPrefix(String urlPrefix) {

    }

    @Override
    public String getPath() {
        return null;
    }

    @Override
    public int getPort() {
        return 0;
    }

    @Override
    public String getUrlPrefix() {
        return null;
    }

    @Override
    public void setPath(String path) {

    }

    @Override
    public void setHost(String host) {

    }

    @Override
    public void setPort(int port) {

    }

    @Override
    public String agreement() {
        return null;
    }

    @Override
    public void setUrl(String url) {
        HttpRequestWrapper httpRequestWrapper = HttpRequestWrapper.class.cast(httpRequest);
        URI uri = httpRequestWrapper.getURI();
        URIBuilder uriBuilder = new URIBuilder(uri);
        uriBuilder.setPath(url);
        try {
            httpRequestWrapper.setURI(uriBuilder.build());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        this.httpRequest = httpRequestWrapper;
    }

    @Override
    public boolean isHttps() {
        HttpRequestWrapper httpRequestWrapper = HttpRequestWrapper.class.cast(httpRequest);
        URI uri = httpRequestWrapper.getURI();
        return uri.getHost().toUpperCase(Locale.ROOT).startsWith("HTTPS");
    }

    @Override
    public Map<String, String> getHeader() {
        Map<String, String> headerMap = new HashMap<>();
        Header[] allHeaders = httpRequest.getAllHeaders();
        for (Header header : allHeaders) {
            headerMap.put(header.getName(), header.getValue());
        }
        return headerMap;
    }

    @Override
    public void addHeader(String k, String v) {
        httpRequest.addHeader(k,v);
    }

    @Override
    public Map<String, String> getUrlParameter() {
        HttpRequestWrapper httpRequestWrapper = HttpRequestWrapper.class.cast(httpRequest);
        URI uri = httpRequestWrapper.getURI();
        String query = uri.getQuery();
        return null;
    }

    @Override
    public ContentType getContentType() {
        return null;
    }

    @Override
    public HttpType getHttpType() {
        return null;
    }

    @Override
    public void setHttpType(HttpType httpType) {

    }

    @Override
    public Map<String, String> getFormParameter() {
        return null;
    }

    @Override
    public String getBody() {
        return null;
    }

    @Override
    public void setBody(String body, ContentType contentType) {

    }

    @Override
    public void addUrlParameter(String k, String v) {

    }

    @Override
    public void addUrlParameter(Map<String, String> urlParameter) {

    }

    @Override
    public void rewriteUrlParameter(Map<String, String> urlParameter) {

    }
}
